
package com.chuji.crmcj.act;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.fragment.ClientLoseFragment;
import com.chuji.crmcj.fragment.FollowTaskFragment;
import com.chuji.crmcj.fragment.OrderLoseFragment;
import com.chuji.crmcj.fragment.ReceiveTaskFragment;
import com.chuji.crmcj.utils.Jump;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.view.ListViewForScrollView;
import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;
import java.util.List;

public class TodayTaskActivity extends BaseActivity{
    private ScrollView mSvScroll;
    //返回
    private ImageView iv_Back;
    /*接待任务*/
    private Button btn_receive_task;
    /*跟进任务*/
    private Button btn_follow_up_task;
    /*蓝色下划线*/
    private View follow_task_indicate_line;
    /*屏幕宽度*/
    private int screenW;
    //显示
    private ViewPager vp_today_task;
    /*碎片集合*/
    private List<Fragment> fragments;
    float y;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_today_task);
        //初始化组件
        iv_Back= (ImageView) findViewById(R.id.iv_Back);
        btn_receive_task= (Button)findViewById(R.id.btn_receive_task);
        btn_follow_up_task = (Button) findViewById(R.id.btn_follow_up_task);
        follow_task_indicate_line = findViewById(R.id.follow_task_indicate_line);
        vp_today_task = (ViewPager) findViewById(R.id.vp_today_task);
        //数据
        InitTextView();
        vp_today_task.setOnPageChangeListener(new OnMainPageChangeListener());
        //按钮点击事件
        btn_receive_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vp_today_task.setCurrentItem(0);
            }
        });
        btn_follow_up_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vp_today_task.setCurrentItem(1);
            }
        });
        /**
         * 返回
         */
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }
    /**
     * 数据
     */
    protected void InitTextView() {
           /*详情界面的两个功能  填充效果*/
        fragments = new ArrayList<Fragment>();
        fragments.add(new ReceiveTaskFragment());
        fragments.add(new FollowTaskFragment());
        MyPageAdapter myPageAdapter = new MyPageAdapter(getSupportFragmentManager(),fragments);
        vp_today_task.setAdapter(myPageAdapter);
        // 高亮第一个标签
        updateTabs(0);
        // 初始化指示器宽度
        int screenW = getWindowManager().getDefaultDisplay().getWidth();
        follow_task_indicate_line.getLayoutParams().width = screenW / 2;
        follow_task_indicate_line.requestLayout(); // 重新计算大小，并刷新控件
    }
    /**
     * 高亮position选中页面对应的标签，并将其他的变暗
     */
    private void updateTabs(int position) {
        updateTab(position, 0, btn_receive_task);
        updateTab(position, 1, btn_follow_up_task);
    }
    /**
     * 判断当前要处理的 tabPosition 是否是选中的 position，并修改tab的高亮状态
     */
    private void updateTab(int position, int tabPosition, TextView tab) {
        int blue = getResources().getColor(R.color.blue);
        int halfWhite = getResources().getColor(R.color.font3);
        if (position == tabPosition) {
            // tab对应的页面被选中
            tab.setTextColor(blue);
        } else {
            // tab对应的页面没有被选中
            tab.setTextColor(halfWhite);
        }
    }
    public class MyPageAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
    private class OnMainPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        /** 当touch事件发生时回调此方法 */
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // 偏移位置 = 手指划过屏幕的百分比 * 指示器宽度
            int offsetX = (int) (positionOffset * follow_task_indicate_line.getWidth());
            // 起始位置 = position * 指示器宽度
            int startX = position * follow_task_indicate_line.getWidth();
            // 指示器移动的位置 = 起始位置 + 偏移位置
            int translationX = startX + offsetX;
            ViewHelper.setTranslationX(follow_task_indicate_line, translationX);
        }

        @Override
        /** 当见面选中状态发生变更时会回调此方法 */
        public void onPageSelected(int position) {
            // 高亮选中页面对应的标签，并将其他的变暗
            updateTabs(position);
        }

        @Override
        /** 当页面的滑动状态发生变更会回调此方法 */
        public void onPageScrollStateChanged(int state) {

        }
    }

}

